//
//  HMMeTableViewController.m
//  HowToDo
//
//  Created by Apple on 16/12/31.
//  Copyright © 2016年 KY. All rights reserved.
//

#import "HMLoginMainViewController.h"
#import "HMMeTableViewController.h"
#import "Masonry.h"
#import "UILabel+Addition.h"
#import "HMSetModel.h"
#import "HMSettingViewController.h"
#import "HMDefualtViewController.h"
#import "HMSetInformationViewController.h"
#import "HMPayViewController.h"
#import "HMLearnTimeController.h"
#import "HMShoppingCarTableViewController.h"
#import "HMDownLoadViewController.h"
#import "HMCDKController.h"
#define  kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define  kScreenHeight  [UIScreen mainScreen].bounds.size.height
#define  KbackImageViewH 240
#define  kbaseCellHeight 54
#define  kinterval 8
#define  ktranslationY 100
@interface HMMeTableViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
///背景可所放照片
@property (nonatomic,strong)UIImageView *backImageView;
///展示的菜单列表
@property (nonatomic,strong)UITableView *displayTableView;
///displayTableView的父控件
@property (nonatomic, strong) UIView *bgView;
///bgView的父控件
@property (nonatomic, strong) UIScrollView *scrollView;
///displayTableView的头视图
@property (nonatomic, strong) UIView *headerView;
///头视图上的图
@property (nonatomic, strong) UIView *coverView;
///菜单列表模型
@property (nonatomic, strong) HMSetModel *model;
///头像
@property (nonatomic, strong) UIImageView * headView;
///账户余额控制器
@property (nonatomic, strong) HMPayViewController * payVC;
///显示昵称
@property (nonatomic, strong) UILabel *nameLable;


@end

@implementation HMMeTableViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

#pragma mark -懒加载-
- (HMPayViewController *)payVC{
    if(!_payVC){
        _payVC = [[HMPayViewController alloc] init];
    }
    return _payVC;
}

- (UITableView*)displayTableView
{
    if (!_displayTableView) {
        _displayTableView  = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _displayTableView.delegate = self;
        _displayTableView.dataSource = self;
        _displayTableView.showsVerticalScrollIndicator = NO;
        _displayTableView.separatorStyle = NO;
    }
    return _displayTableView;
}

- (UIImageView *)backImageView
{
    if (!_backImageView)
    {
        _backImageView = [UIImageView new];
        _backImageView.image = [UIImage imageNamed:@"userinfo_top_bg"];
        _backImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backImageView.clipsToBounds = YES;
    }
    return _backImageView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    }
    return _scrollView;
}

- (UIView *)bgView{
    if(!_bgView){
        _bgView = [UIView new];
    }
    return _bgView;
}

- (UIView*)headerView
{
    if (!_headerView) {
        _headerView = [UIView new];
        _headerView.userInteractionEnabled = YES;
        _headerView.frame = CGRectMake(0, 0, kScreenWidth,kScreenWidth / 3);
    }
    return _headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //  监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"loginSuccess" object:nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.displayTableView.clipsToBounds = NO;
    
    [self setMenuBound];
    
    [self makeLable];
    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByOne];
    //-------------设置导航栏结束--------------------------------
}

- (void)loadData {
    //  请求我的这个页面的数据
    NSLog(@"login");
    
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //隐藏导航栏
    [self.navigationController setNavigationBarHidden:YES];
}


///设置菜单约束
- (void)setMenuBound{
    [self.view addSubview:self.backImageView];
    [_backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.offset(KbackImageViewH);
    }];
    
    [self.view addSubview:self.scrollView];
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width,kbaseCellHeight * 10);
    
    [_scrollView addSubview:self.bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(_scrollView);
        make.top.equalTo(_scrollView.mas_top).offset(120);
        make.width.offset(kScreenWidth);
        make.height.offset(kbaseCellHeight * 17 + kinterval * 3);
    }];
    _scrollView.delegate = self;
    
    [self consummateHeadView];
    self.displayTableView.tableHeaderView = self.headerView;
    
    self.headerView.backgroundColor = [UIColor clearColor];
    self.displayTableView.backgroundColor = [UIColor clearColor];
    
    [_bgView addSubview:self.displayTableView];
    [_displayTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(_bgView);
        make.width.offset(kScreenWidth);
        make.height.equalTo(_bgView);
    }];
}

///完善头视图
- (void)consummateHeadView{
    self.coverView = [[UIView alloc] init];
    [self.headerView addSubview:_coverView];
    [_coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.headerView);
        make.height.offset(ktranslationY);
    }];
    _coverView.backgroundColor = [UIColor whiteColor];
    
    UIImageView * headView = [[UIImageView alloc] init];
    [_coverView addSubview:headView];
    headView.backgroundColor = [UIColor lightGrayColor];
    
    NSString * path = [[HMSetInformationViewController sharedInfomationVC] getPhotoFilePath];
    UIImage * pathImage = [UIImage imageWithContentsOfFile:path];
    headView.image = pathImage;
    

    
    //切圆
    headView.layer.cornerRadius = 25;
    headView.clipsToBounds = YES;
    [headView.layer masksToBounds];
    
    headView.contentMode = UIViewContentModeScaleToFill;
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_coverView);
        make.centerY.equalTo(_coverView.mas_top);
        make.width.height.offset(ktranslationY * 0.5);
    }];
    headView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewtapGesture:)];
    [headView addGestureRecognizer:tapGesture];
    self.headView = headView;
}

///头像等详细信息的设置
- (void)headViewtapGesture:(UITapGestureRecognizer *)sender{
    __weak typeof(self) weakSelf = self;
    [HMSetInformationViewController sharedInfomationVC].recallImageBlock = ^(UIImage *image){
        weakSelf.headView.image = image;
    };
    [HMSetInformationViewController sharedInfomationVC].recallNameStringBlock = ^(NSString *nameString){
        weakSelf.nameLable.text = nameString;
    };
    [self.navigationController pushViewController:[HMSetInformationViewController sharedInfomationVC] animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
}


///创建lable按钮
- (void)makeLable{
    UILabel * learnTimeLable = [UILabel hm_labelWithText:@"学习时间"fontSize:k_FONT_SMALL color:[UIColor darkGrayColor] TextAlignment:NSTextAlignmentCenter];

    UILabel * readBookLable = [UILabel hm_labelWithText:@"阅读书籍" fontSize:k_FONT_SMALL color:[UIColor darkGrayColor] TextAlignment:NSTextAlignmentCenter];
    
    UILabel * continuouStudyLable = [UILabel hm_labelWithText:@"连续学习" fontSize:k_FONT_SMALL color:[UIColor darkGrayColor] TextAlignment:NSTextAlignmentCenter];
    
    UILabel * recordLearnTimeLable = [UILabel hm_labelWithText:@"8小时17分" fontSize:k_FONT_NORMAL color:[UIColor blackColor] TextAlignment:NSTextAlignmentCenter];
    
    UILabel * recordReadBookLable = [UILabel hm_labelWithText:@"2本" fontSize:k_FONT_NORMAL color:[UIColor blackColor] TextAlignment:NSTextAlignmentCenter];
    
    UILabel * recordContinuouStudyLable = [UILabel hm_labelWithText:@"0天" fontSize:k_FONT_NORMAL color:[UIColor blackColor] TextAlignment:NSTextAlignmentCenter];
    
    NSString *str2 = [[HMSetInformationViewController sharedInfomationVC] getPhotoFilePathWithNSString:@"name"];
    NSString *str = [NSString stringWithContentsOfFile:str2 encoding:NSUTF8StringEncoding error:nil];
    
    UILabel * nameLable = [UILabel hm_labelWithText:str fontSize:k_FONT_NORMAL color:[UIColor blackColor]];
    
    [_coverView addSubview:learnTimeLable];
    [_coverView addSubview:readBookLable];
    [_coverView addSubview:continuouStudyLable];
    [_coverView addSubview:recordLearnTimeLable];
    [_coverView addSubview:recordReadBookLable];
    [_coverView addSubview:recordContinuouStudyLable];
    [_coverView addSubview:nameLable];
    
    [learnTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coverView.mas_left).offset(kinterval * 2);
        make.bottom.equalTo(_coverView.mas_bottom).offset(-kinterval);
    }];
    
    [readBookLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(learnTimeLable);
        make.width.equalTo(learnTimeLable.mas_width);
        make.left.equalTo(learnTimeLable.mas_right).offset(0);
    }];
    
    [continuouStudyLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_coverView.mas_right).offset(-kinterval * 2);
        make.centerY.equalTo(learnTimeLable);
        make.width.equalTo(learnTimeLable.mas_width);
        make.left.equalTo(readBookLable.mas_right).offset(0);
    }];
    
    [recordLearnTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coverView.mas_left).offset(kinterval * 2);
        make.bottom.equalTo(learnTimeLable.mas_top).offset(-kinterval);
    }];
    
    [recordReadBookLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(recordLearnTimeLable);
        make.width.equalTo(recordLearnTimeLable.mas_width);
        make.left.equalTo(recordLearnTimeLable.mas_right).offset(0);
    }];
    
    [recordContinuouStudyLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_coverView.mas_right).offset(-kinterval * 2);
        make.centerY.equalTo(recordLearnTimeLable);
        make.width.equalTo(recordLearnTimeLable.mas_width);
        make.left.equalTo(recordReadBookLable.mas_right).offset(0);
    }];
    
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_coverView);
        make.bottom.equalTo(recordReadBookLable.mas_top).offset(-kinterval);
    }];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewtapGesture:)];
    [nameLable addGestureRecognizer:tapGesture];
    
    nameLable.userInteractionEnabled = YES;
    
    self.nameLable = nameLable;
    
    [self coverClarityView];
}

#pragma mark -代理-
///设置cell之间的间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

///返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

///返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 3){
        return 3;
    }
    else if(section == 4){
        return 5;
    }
    else if(section == 5){
        return 2;
    }
    return 1;
}

///cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kbaseCellHeight;
}

///cell内容
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentity = @"cellID";
    
    UITableViewCell * tableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
    if (!tableViewCell) {
        tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentity];
    }
    
    if(indexPath.section == 0){
        self.model = [HMSetModel loadSetMenuData][indexPath.row];
    }
    else if (indexPath.section == 1){
        self.model = [HMSetModel loadSetMenuData][indexPath.row + 1];
    }
    else if (indexPath.section == 2){
        self.model = [HMSetModel loadSetMenuData][indexPath.row + 2];
    }
    else if (indexPath.section == 3){
        self.model = [HMSetModel loadSetMenuData][indexPath.row + 3];
    }
    else if (indexPath.section == 4){
        self.model = [HMSetModel loadSetMenuData][indexPath.row + 6];
        if(indexPath.row == 0){
            if(!self.payVC.payDetailed){
                tableViewCell.detailTextLabel.text = @"0.00";
            }
            self.payVC.payDetailed = ^(int payNumber){
                NSString * numstr = [NSString stringWithFormat:@"%d",payNumber];
                tableViewCell.detailTextLabel.text = numstr;
            };
        }
    }
    else if (indexPath.section == 5){
        self.model = [HMSetModel loadSetMenuData][indexPath.row + 11];
    }
    
    tableViewCell.textLabel.text = self.model.name;
    tableViewCell.imageView.image = [UIImage imageNamed:self.model.icon];
    
    [tableViewCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return tableViewCell;
}

///取消选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HMSettingViewController * settingVC = [[HMSettingViewController alloc] init];
    //点击设置
    if(indexPath.section == 5 && indexPath.row == 1){
        settingVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:settingVC animated:YES];
    }
    //点击余额
    else if(indexPath.section == 4 && indexPath.row == 0){
        [self.navigationController pushViewController:self.payVC animated:YES];
    }
    //点击购物车
    else if (indexPath.section == 4 && indexPath.row == 3){
        HMShoppingCarTableViewController * shopCarVC = [[HMShoppingCarTableViewController alloc] init];
        [self.navigationController pushViewController:shopCarVC animated:YES];
    }
//    else if (indexPath.section == 3 && indexPath.row == 1){
//        HMDownLoadViewController * downloadVC = [[HMDownLoadViewController alloc] init];
//        [self.navigationController pushViewController:downloadVC animated:YES];
//    }
    //点击账号
    else if(indexPath.section == 5 && indexPath.row == 0){
        
        HMLoginMainViewController *loginMainVC = [[HMLoginMainViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginMainVC];
        
        [self presentViewController:nav animated:YES completion:nil];
        NSLog(@"%@",self.presentedViewController);
    }
    //兑换券
    else if(indexPath.section == 4 && indexPath.row == 1){
        HMCDKController *cdkVC = [[HMCDKController alloc] init];
        [self.navigationController pushViewController:cdkVC animated:YES];
    }
    else{
        HMDefualtViewController * baseVC = [[HMDefualtViewController alloc] init];
        [baseVC showTipViewWithName:nil andMessage:@"没有相关信息,真抱歉!"];
        [self.navigationController pushViewController:baseVC animated:YES];
    }
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
}

///背景图片缩放
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat imageW = kScreenWidth;
    if (offset_Y < -ktranslationY)
    {
        CGFloat totalOffset = KbackImageViewH * 0.5 + ABS(offset_Y) + ktranslationY * 0.25;
        CGFloat f = totalOffset / KbackImageViewH;
        self.backImageView.frame = CGRectMake(-(imageW * f - imageW) * 0.5, 0, imageW * f, totalOffset);
    }
}

//被点击来获取学习时间的模板
- (void)coverClarityView{
    UIView *coverClarityView = [[UIView alloc] init];
    [self.coverView addSubview:coverClarityView];
    
    [coverClarityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.coverView);
        make.height.offset(60);
    }];
    coverClarityView.backgroundColor = [UIColor clearColor];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClarityViewClick:)];
    [coverClarityView addGestureRecognizer:tapGesture];
}

- (void)coverClarityViewClick:(UITapGestureRecognizer *)sender{
    HMLearnTimeController *learnTimeVC = [[HMLearnTimeController alloc] init];
    [self.navigationController pushViewController:learnTimeVC animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
}
@end
